package com.cskaoyan;

import com.cskaoyan.utils.C3p0Utils;
import com.cskaoyan.utils.DBCPUtils;
import com.cskaoyan.utils.DruidUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 创建日期: 2022/10/10 15:13
 *
 * @author ciggar
 */
public class DatasourceDemo {

    public static void main(String[] args) throws SQLException {

        // 1. 从数据库连接池获取连接
        // JDBC4Connection
        // DBCP: PoolingDatasource$PoolGuardConnectionWrapper（内部类）
        // C3p0: NewProxyConnection
        // Druid: DruidPooledConnection

        Connection connection = C3p0Utils.getConnection();


        // 2. 获取statement对象
        Statement statement = connection.createStatement();


        // 3. 发送SQL语句
        ResultSet resultSet = statement.executeQuery("select * from account");


        // 4. 解析结果集
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int money = resultSet.getInt("money");

            System.out.println("id:" + id + ", name:" + name + ", money :" + money);
        }


        // 5. 关闭部分资源，返回连接
        resultSet.close();
        statement.close();
        connection.close();


    }


    // 指令重排序
    // 如何禁止指令重排序呢？ volatile
    public void test(){

        int m = 1;
        int n = 2;

        int i = m+n;

    }

}
